﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Tomahawk.Runtime.Timeline
{
    public class BlurEffectTrack : SpriteEffectTrack
    {
        [XmlElement("BlurScale")]
        public string blurScale = "";

        [XmlElement("GrayLevel")]
        public string grayLevel = "";

        [XmlElement("SepiaLevel")]
        public string sepiaLevel = "";

        private int width = 1280;
        private int height = 720;
        private float blur = 0.0f;
        private float gray = 0.0f;
        private float sepia = 0.0f;
        private Curve curveBlur = new Curve();
        private Curve curveGray = new Curve();
        private Curve curveSepia = new Curve();

        public BlurEffectTrack() : base() { }

        public BlurEffectTrack(string name) : base(name) { }

        protected override string GetEffectFile()
        {
            return @"Tomahawk.Content\SpriteEffects\BlurEffect";
        }

        protected override void OnLoadEffectParameters(Effect effect)
        {
            effect.Parameters["BlurScale"].SetValue(blur);
            effect.Parameters["GrayLevel"].SetValue(gray);
            effect.Parameters["SepiaLevel"].SetValue(sepia);
        }

        protected override bool OnLoad(GraphicsDevice graphics, ContentManager contentManager)
        {
            width = graphics.PresentationParameters.BackBufferWidth;
            height = graphics.PresentationParameters.BackBufferHeight;

            curveBlur = LoadCurve(contentManager, blurScale);
            curveGray = LoadCurve(contentManager, grayLevel);
            curveSepia = LoadCurve(contentManager, sepiaLevel);

            return base.OnLoad(graphics, contentManager);
        }

        public override void SortKeys() { }

        public override bool Update(float time)
        {
            blur = curveBlur != null ? curveBlur.Evaluate(time) / Math.Min(width, height) : 0.0f;
            gray = curveGray != null ? curveGray.Evaluate(time) : 0.0f;
            sepia = curveSepia != null ? curveSepia.Evaluate(time) : 0.0f;

            return true;
        }
    }
}
